<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use think\Request;
use think\Response;

class Loan extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return Response
     */
    public function index()
    {
        //
        $this->checkLogin();
        if (!$this->auth->check('view_loan', $this->user->id)) return $this->error('权限不足');
        $page = $this->request->param('page', 1);
        $data = \app\model\Loan::page($page, 50)->select();
        return $this->success('success', $data);
    }

    /**
     * 保存新建的资源
     *
     * @param Request $request
     * @return Response
     */
    public function save(Request $request)
    {
        //
        $this->checkLogin();
        if (!$this->auth->check('create_loan', $this->user->id)) return $this->error('权限不足');
        $data = $this->request->post(['user_id', 'record_user_id', 'goods_list', 'voucher']);
        $loan = \app\model\Loan::create($data);
        return $this->success('success', $loan);
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return Response
     */
    public function read($id)
    {
        //
        $this->checkLogin();
        if (!$this->auth->check('view_loan', $this->user->id)) return $this->error('权限不足');
        $loan = \app\model\Loan::find($id);
        if (empty($loan)) return $this->error('资源不存在');
        return $this->success('success', $loan);
    }

    /**
     * 保存更新的资源
     *
     * @param Request $request
     * @param int $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
        $this->checkLogin();
        if (!$this->auth->check('edit_loan', $this->user->id)) return $this->error('权限不足');
        $data = $this->request->post(['user_id', 'record_user_id', 'verify_user_id', 'goods_list', 'voucher', 'status', 'reject_reason']);
        $loan = \app\model\Loan::find($id);
        if (empty($loan)) return $this->error('资源不存在');
        $loan->save($data);
        return $this->success('success', $loan);
    }

    public function pass($id)
    {
        $this->checkLogin();
        if (!$this->auth->check('examine_loan', $this->user->id)) return $this->error('权限不足');
        $loan = \app\model\Loan::find($id);
        if (empty($loan)) return $this->error('资源不存在');
        $loan->status = 1;
        $loan->save();
        return $this->success('pass success');
    }

    public function reject(Request $request, $id)
    {
        $this->checkLogin();
        if (!$this->auth->check('examine_loan', $this->user->id)) return $this->error('权限不足');
        $loan = \app\model\Loan::find($id);
        if (empty($loan)) return $this->error('资源不存在');
        $loan->status = -1;
        $loan->reject_reason = $request->patch('reject_reason');
        $loan->save();
        return $this->success('reject success');
    }

//    /**
//     * 删除指定资源
//     *
//     * @param int $id
//     * @return \think\Response
//     */
//    public function delete($id)
//    {
//        //
//    }
}
